<template>
  <div>
    <div class="select_top">
      <el-select v-model="codeflag" placeholder="工资类型" clearable @change="changeflag">
        <el-option
          v-for="item in wageType"
          :key="item.hash.code_id"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>
      <el-select v-model="wdate" style="margin:0 10px" placeholder="日期"
        :disabled="codeflag==''"
        @change="gettableList"
      >
        <el-option
          v-for="item in wdateList"
          :key="item.hash.wdate"
          :label="item.hash.wdate"
          :value="item.hash.wdate"
        ></el-option>
      </el-select>


      <el-button size="mini" @click="gettableList" type="primary">查询</el-button>
      <el-button size="mini" style="float:right" type="primary" @click="docheck">发布工资</el-button>


    </div>
    <wagelist :tableList="tableList" :headList="headList" v-if="alive"></wagelist>
  </div>
</template>
<script>
import wagelist from '~/components/list/wageList.vue';

export default {
  data() {
    return {
      ts: this.newStore(),
      alive: true,
      codeflag: '',
      wdate: '',
      wdateList: [],
      wageType: [],
      tableList: [],
      headList: [],
      orderlist: [],
      tableCopyTableList: [],
    };
  },
  components: {
    wagelist,
  },
  created() {
    this.getwageType();
    // this.gettableList();
  },
  methods: {
    docheck() {
      if (this.codeflag == '') {
        this.$message({
          type: 'error',
          message: '请先选择工资类型',
        });
        return;
      }
      if (this.wdate == '') {
        this.$message({
          type: 'error',
          message: '请先选择参考日期',
        });
        return;
      }
      const req = {
        handle: '5A4A3E4E20E0D8E9B533F46965D06DBC340247C3D6B20069B64C71FE28AFA44C',
        wcodeflag: this.codeflag,
        wdate: this.wdate,
      };
      this.$confirm('确定发布工资?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.spost(this.ts, '/mainservlet', req).then((ts) => {
          console.log(ts.d);
          this.$message({
            type: 'success',
            message: '发布成功',
          });
          this.changeflag(this.codeflag);
          this.codeflag = '';
          this.wdate = '';
          this.tableList = [];
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消',
        });
      });
    },
    changeflag(val) {
      this.wdate = '';
      this.tableList = [];
      this.reload();
      const req = {
        handle: '5A4A3E4E20E0D8E9F649FA99F50A3FE0D12821C7AB79A383C85F6131D883C036A4FAC5F547223AE6',
        wcodeflag: val,
        wstatus: '0',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        console.log(ts.d);
        this.wdateList = ts.d.data.operation1;
      });
    },
    reload() {
      this.alive = false;
      this.$nextTick(() => {
        this.alive = true;
      });
    },
    // 获取工资类型
    getwageType() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '1',
        codetype: '02',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        console.log(ts.d);
        this.wageType = ts.d.data.operation1;
      });
    },
    // 获取工资查询列表
    gettableList() {
      console.log('in gettableList');

      if (this.codeflag == '') {
        this.$message({
          type: 'error',
          message: '请先选择工资类型',
        });
        return;
      }
      if (this.wdate == '') {
        this.$message({
          type: 'error',
          message: '请先选择日期',
        });
        return;
      }

      this.headList = [];
      const req = {
        handle:
          '5A4A3E4E20E0D8E9DF4D9F70BB944481CBA5FDB6D7C6913509D8A72CF4F6A23E6B1DD5DC9EAAD578',
        wcodeflag: this.codeflag,
        wdate: this.wdate,
        wstatus: '0',
        worgname: '',
        uname: '',
      };
      this.spost(this.ts, '/mainservlet', req)
        .then((ts) => {
          this.tableList = ts.d.data.operation1;

          const req1 = {
            handle: '5A4A3E4E20E0D8E9F649FA99F50A3FE0D066FF5E6CBBF3F214D6ED002B02A0EC',
            wcodeflag: this.codeflag,
            wdate: this.wdate,
            query: '',
            start: '0',
            limit: '1000000',
          };
          this.spost(this.ts, '/mainservlet', req1).then((ts1) => {
            this.orderlist = ts1.d.data.operation1;
            for (let i = 0; i < this.orderlist.length; i += 1) {
              this.headList.push(this.orderlist[i].hash.codename);
            }

            if (this.tableList.length > 0) {
              for (let j = 0; j < this.headList.length; j += 1) {
                let tused = false;

                for (const key in this.tableList[0].hash) {
                  if (
                    key != 'id' &&
                  key != 'uid' &&
                  key != 'uname' &&
                  key != 'wdate' &&
                  key != 'worgname' &&
                  key != 'worgid' &&
                  key != 'wstatus'
                  ) {
                    if (key == this.headList[j]) {
                      tused = true;
                      break;
                    }
                  }
                }

                if (tused == false) {
                  this.headList.splice(j, 1);
                  j -= 1;
                }
              }
            }
            this.reload();
          });
        });
    },
  },
};
</script>

